﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.SPECT;
using Mang24k99.Business.SPECT;
using Castle.ActiveRecord;
using Wuqi.Webdiyer;

namespace Mang24k99.Web.SpecialManage
{
    public partial class SpecialSubType : System.Web.UI.Page
    {
        string _AspNetName = "CustomInfoHTML";
        public string AspNetCustomInfoHTML
        {
            get
            {
                return Common.ViewStateHelper.GetString(this.ViewState, _AspNetName);
            }
            set
            {
                Common.ViewStateHelper.SetViewState(this.ViewState, value, _AspNetName);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AspNetCustomInfoHTML = this.AspNetPager1.CustomInfoHTML;
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;
                BindAspNetPager();
                BinDate();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Session["Action"]!=null)
            {
                if (Session["Action"].ToString()=="Insert")
                {  //验证类型编号是否重复
                    string where = "subtype.SubjectType='" + this.txtType.Text.Trim() + "'";
                    if (RepeatValidate(where)==false)
                    {
                        string strMsg = "类型编号不能重复！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    //验证类型名称是否重复
                    where = "subtype.SubjectTypename='" + this.txtName.Text.Trim() + "'";
                    if (RepeatValidate(where)==false)
                    {
                        string strMsg = "类型名称不能重复！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    MKtSpectSubtype subtype = new MKtSpectSubtype();
                    subtype.SubjectType = Convert.ToInt32(this.txtType.Text.Trim());
                    subtype.SubjectTypename = this.txtName.Text.Trim();
                    subtype.TitleDisplay = this.txtTitleDisplay.Text.Trim();
                    subtype.AddresDsplay = this.txtAddrssDisplay.Text.Trim();
                    subtype.DisplayNum = Convert.ToInt32(this.txtNum.Text.Trim());
                    subtype.IsValidate = 1;
                    subtype.InputTime = DateTime.Now;
                    
                    try
                    {
                        BKtSpectSubtype.Create(subtype);
                        string strMsg = "新增成功！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        this.Panel1.Visible = false;
                        this.Panel2.Visible = true;
                        ClearPage();
                        BindAspNetPager();
                        BinDate();
                        Session["Action"] = "";
                    }
                    catch (System.Exception ex)
                    {
                        throw;
                    }
                }
                else if (Session["Action"].ToString()=="Update")
                {
                    //验证类型编号是否重复
                    string where = "subtype.SubjectType='" + this.txtType.Text.Trim() + "' and subtype.SubjectType<>" +this.txtHType.Text.Trim();
                    if (RepeatValidate(where) == false)
                    {
                        string strMsg = "类型编号不能重复！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    //验证类型名称是否重复
                    where = "subtype.SubjectTypename='" + this.txtName.Text.Trim() + "' and subtype.SubjectType <>" + this.txtHType.Text.Trim();
                    if (RepeatValidate(where)==false)
                    {
                        string strMsg = "类型名称不能重复！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    using (TransactionScope tran = new TransactionScope())
                    {
                        try
                        {
                            MKtSpectSubtype subtype = MKtSpectSubtype.SelectByPrimaryKey(Convert.ToInt32(this.txtHType.Text));
                            // 如果新设置的显示条目比原有条目小，则删除数据库中多余记录
                            if (subtype.DisplayNum - Convert.ToInt32(this.txtNum.Text)>0)
                            {
                                int type = Convert.ToInt32(txtType.Text.Trim());
                                MKtSpectSubtype.DeleteByOrder(Convert.ToInt32(this.txtNum.Text.Trim()),Convert.ToInt32(subtype.DisplayNum),type);
                            }
                            subtype.SubjectTypename = this.txtName.Text.Trim();
                            subtype.TitleDisplay = this.txtTitleDisplay.Text.Trim();
                            subtype.AddresDsplay = this.txtAddrssDisplay.Text.Trim();
                            subtype.DisplayNum = Convert.ToInt32(this.txtNum.Text.Trim());
                            subtype.ModifyTime = DateTime.Now;
                            BKtSpectSubtype.Update(subtype);
                            string strMsg = "修改成功！";
                            Common.MessageBox.Show(this.Page, strMsg);
                            this.Panel1.Visible = false;
                            this.Panel2.Visible = true;
                            ClearPage();
                            BinDate();
                            Session["Action"] = "";
                            tran.VoteCommit();
                        }
                        catch
                        {
                            tran.VoteRollBack();
                        }
                    }
                }
            }

        }
        /// <summary>
        /// 绑定gridview
        /// </summary>
        public void BinDate()
        {
            AspNetPager aspNetPager = this.AspNetPager1;
            int maxRecord = aspNetPager.PageSize;
            int startRecord = aspNetPager.StartRecordIndex - 1;
            object dataSource = BKtSpectSubtype.FindALLSliced(startRecord, maxRecord);
            this.grdList.DataSource = dataSource;
            this.grdList.DataBind();
        }
        /// <summary>
        /// Binds the ASP net pager.(绑定分页控件)
        /// </summary>
        private void BindAspNetPager()
        {
            AspNetPager aspNetPager = this.AspNetPager1;
            int RecordCount = BKtSpectSubtype.GetCount();
            aspNetPager.RecordCount = RecordCount;
            aspNetPager.CustomInfoHTML = string.Format(AspNetCustomInfoHTML, aspNetPager.PageCount);
        }

        /// <summary>
        /// Handles the PageChanged event of the AspNetPager1 control.(列表改变时候)
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindAspNetPager();
            BinDate();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["Action"]!=null)
            {
                if (Session["Action"].ToString()=="Insert")
                {
                    ClearPage();
                }
                if (Session["Action"].ToString()=="Update")
                {
                    InItDate(Convert.ToInt32(this.txtHType.Text.Trim()));
                }
            }
        }
        //清空页面
        public void ClearPage()
        {
            this.txtAddrssDisplay.Text = "";
            this.txtName.Text = "";
            this.txtNum.Text = "";
            this.txtTitleDisplay.Text = "";
            this.txtType.Text = "";
            this.txtHType.Text = "";
            this.txtType.ReadOnly = false;
        }

        /// <summary>
        /// 删除操作(逻辑删除)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void grdList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MKtSpectSubtype subtype = new MKtSpectSubtype();
            int specialtype = Int32.Parse(this.grdList.Rows[e.RowIndex].Cells[1].Text);
            if (specialtype<=4)
            {
                string strMsg = "固定专题类型，不允许删除！";
                Common.MessageBox.Show(this.Page, strMsg);
                return;
            }
            subtype = MKtSpectSubtype.SelectByPrimaryKey(specialtype);
            bool result = BKtSpectSubtype.Delete(subtype);
            if (result == true)
            {
                string strMsg = "删除成功！";
                Common.MessageBox.Show(this.Page, strMsg);
                BindAspNetPager();
                BinDate();
            }
            else
            {
                string strMsg = "删除失败！";
                Common.MessageBox.Show(this.Page, strMsg);
            }
        }
        /// <summary>
        /// 检查类别名称或类别编号是否重复
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public bool RepeatValidate(string where)
        {
            MKtSpectSubtype[] typelist = (MKtSpectSubtype[])BKtSpectSubtype.SelectByWhere(where);
            if (typelist.Length>0)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 点击编辑，修改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index =this.grdList.SelectedIndex;
            int subjectType = Convert.ToInt32(grdList.Rows[index].Cells[1].Text);
            InItDate(subjectType);
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="type"></param>
        public void InItDate(int type)
        {
            MKtSpectSubtype subtype = MKtSpectSubtype.SelectByPrimaryKey(type);
            this.txtType.Text = subtype.SubjectType.ToString();
            this.txtAddrssDisplay.Text = subtype.AddresDsplay;
            this.txtName.Text = subtype.SubjectTypename;
            this.txtNum.Text = subtype.DisplayNum.ToString();
            this.txtHType.Text = type.ToString();
            this.txtTitleDisplay.Text = subtype.TitleDisplay;
            Session["Action"] = "Update";
            this.txtType.ReadOnly = true;
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            Session["Action"] = "Insert";
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            ClearPage();
        }
    }
}
